package site.isscloud.framework.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import site.isscloud.framework.config.MqttConfig;

@Component
@ConditionalOnProperty(name = "mqtt.enabled", havingValue = "true")
public class MqttService {

    private static final Logger log = LoggerFactory.getLogger(MqttService.class);

    @Autowired
    private MqttClient mqttClient;

    @Autowired
    private MqttConfig mqttConfig;

    public boolean publish(String topic, String message) {
        return mqttConfig.publish(topic, message, 1);
    }

    public boolean publish(String topic, String message, int qos) {
        return mqttConfig.publish(topic, message, qos);
    }

    public boolean isConnected() {
        return mqttConfig.isConnected();
    }

    public void subscribe(String topic, int qos) {
        mqttConfig.subscribe(topic, qos);
    }

    public void unsubscribe(String topic) {
        mqttConfig.unsubscribe(topic);
    }
}